from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.select import Select

driver = webdriver.Firefox()
driver.maximize_window()
driver.get('https://www.douban.com')
# 隐式等待
driver.implicitly_wait(10)
# 强制等待
time.sleep(2)
# 进入frame框架
f = driver.find_element(By.XPATH, '/html/body/div[2]/div/div[1]/iframe')
driver.switch_to.frame(f)
# 第一步：输入手机号
driver.find_element(By.CLASS_NAME, 'account-form-input').send_keys('15985645480')
# 强制等待30秒  输入验证码
time.sleep(30)
# 第二步：点击登录豆瓣
driver.find_element(By.XPATH, '/html/body/div[1]/div[2]/div[1]/div[5]/a').click()
time.sleep(3)
# 退出frame框架
driver.switch_to.default_content()
time.sleep(2)

# 显示等待10 每隔0.5秒查询一次 定位写日记
rj = WebDriverWait(driver, 10, 0.5).until(
    EC.presence_of_element_located((By.XPATH, '//a[@href="https://www.douban.com/note/create"]'))
)
# 第三步： 单击写日记  鼠标单击事件
ActionChains(driver).click(rj).perform()
time.sleep(2)
# 窗口切换
driver.switch_to.window(driver.window_handles[-1])
time.sleep(2)
# 第四步：添加标题
bt = driver.find_element(By.CSS_SELECTOR, '.editor-input')
# 鼠标右击事件
ActionChains(driver).context_click(bt).perform()
time.sleep(2)
# 键盘输入事件
bt.send_keys('day11')
time.sleep(2)
# 鼠标三击事件
ActionChains(driver).double_click(bt).click().perform()
time.sleep(2)
# 键盘复制事件
bt.send_keys(Keys.CONTROL, 'c')
time.sleep(2)
# 第五步：添加正文  显示等待10 每隔0.5秒查询一次 定位正文
zw = WebDriverWait(driver, 10, 0.5).until(
    EC.presence_of_element_located((By.CSS_SELECTOR, '#note-editor-root > div > div.editor-body > div > div > div:nth-child(2) > div > div.DraftEditor-editorContainer > div'))
)
# 键盘粘贴事件
zw.send_keys(Keys.CONTROL, 'v')
time.sleep(2)
# 键盘的回车事件
zw.send_keys(Keys.ENTER)
time.sleep(2)
# 键盘的输入事件
zw.send_keys('今天写的日记：哈哈哈')
time.sleep(2)
# 第六步：点击下一步
xyb = driver.find_element(By.CLASS_NAME, 'editor-extra-button-submit')
# 打印出元素的如下属性值
print(f'文本值：{xyb.text}')
print(f'元素的Class属性：{xyb.text}')
print(f'元素的宽高：{xyb.text}')
print(f'元素的坐标值：{xyb.text}')
print(f'元素是否可用（true表可用）：{xyb.text}')
print(f'元素是否显示（true表显示）：{xyb.text}')
print(f'元素是否被选中（true表被选中）：{xyb.text}')
xyb.click()
time.sleep(2)
# 第七步：点击权限设置
driver.find_element(By.XPATH, '/html/body/div[6]/div/div/div[2]/form/div[3]/div/a').click()
time.sleep(2)
# 第八步：选择隐私设置
s1 = Select(driver.find_element(By.XPATH, '/html/body/div[6]/div/div/div[2]/form/div[3]/div[2]/div[1]/div[2]/select'))
s1.select_by_value('S')
time.sleep(2)
# 第九步：选择回应设置
s2 = Select(driver.find_element(By.XPATH, '/html/body/div[6]/div/div/div[2]/form/div[3]/div[2]/div[2]/div[2]/select'))
s2.select_by_index(1)
# 强制等待15秒 输入验证码
time.sleep(15)
# 第十步：点击提交
driver.find_element(By.XPATH, '/html/body/div[6]/div/div/div[2]/form/div[5]/button').click()
time.sleep(2)
# 第十一步：点击删除
driver.find_element(By.XPATH, '/html/body/div[4]/div[1]/div/div[1]/div[1]/div[4]/div[1]/span[4]/a').click()
time.sleep(2)
# 第十二步：弹窗确定删除
alert = driver.switch_to.alert
alert.accept()

time.sleep(10)


